package dao;

import cn.hutool.crypto.digest.MD5;
import domain.User;
import util.DbUtilHikari;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import cn.hutool.crypto.SecureUtil;

/**
 * 对用户表进行 增删改操作
 * @author 86134
 * @version 1.0
 */
public class UserDao {

    /**
     * 用户登录
     * @param uname
     * @param upsw
     * @return
     */
    public boolean login(String uname,String upsw){

        boolean flag = false;
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        try {
            connection=DbUtilHikari.getConnection();
            String sql = "SELECT * FROM users WHERE username = ? AND userpsw = ?";
            preparedStatement=connection.prepareStatement(sql);
            preparedStatement.setString(1,uname);
            preparedStatement.setString(2,SecureUtil.md5(uname));
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()){
                flag = true;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DbUtilHikari.close(resultSet,preparedStatement,connection);
        }
        return flag;
    }

    /**
     * 添加新用户
     * @param uname
     * @param upsw
     * @return 0 失败 1 成功
     */
    public int insertUser(String uname,String upsw){

        Connection connection = null;
        PreparedStatement preparedStatement = null;
        int flag = 0;

        try {
            connection = DbUtilHikari.getConnection();
            String sql = "insert into users(username,userpsw) values(?,?)";
            preparedStatement=connection.prepareStatement(sql);
            preparedStatement.setString(1,uname);
            preparedStatement.setString(2, SecureUtil.md5(uname));
            flag = preparedStatement.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            if (preparedStatement != null && connection != null){
                DbUtilHikari.close(preparedStatement,connection);
            }
        }

        return flag;
    }

    /**
     * 删除用户
     * @param userid
     * @return 0 失败 1 成功
     */
    public int deleteUser(int userid){
        int flag = 0;
        Connection connection = null;
        PreparedStatement preparedStatement = null;

        try {
            connection = DbUtilHikari.getConnection();
            String sql = "DELETE FROM users WHERE userid = ?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1,userid);
            flag = preparedStatement.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DbUtilHikari.close(preparedStatement,connection);
        }

        return flag;
    }

    /**
     * 修改用户
     * @param userid
     * @param uname
     * @param upsw
     * @return 0 失败 1 成功
     */
    public int updataUser(int userid,String uname,String upsw){
        int flag = 0;
        Connection connection = null;
        PreparedStatement preparedStatement = null;

        try {
            connection = DbUtilHikari.getConnection();
            String sql = "UPDATE users SET username = ?,userpsw = ? WHERE userid = ?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,uname);
            preparedStatement.setString(2, SecureUtil.md5(uname));
            preparedStatement.setInt(3,userid);
            flag = preparedStatement.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DbUtilHikari.close(preparedStatement,connection);
        }
        return flag;
    }

    /**
     * 查询所有用户
     * @return 用户集合
     */
    public List<User> seleteAll(){
        List<User> userList = new ArrayList<User>();
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        try {
            connection=DbUtilHikari.getConnection();
            String sql = "SELECT * FROM users";
            preparedStatement=connection.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
                User user = new User();
                user.setUserid(resultSet.getInt("userid"));
                user.setUsername(resultSet.getString("username"));
                user.setUserpsw(resultSet.getString("userpsw"));
                userList.add(user);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DbUtilHikari.close(resultSet,preparedStatement,connection);
        }
        return userList;
    }
}

